window.onload = function() {
	var client_2 = document.getElementById("client_2");
	var dialog_border_2 = document.getElementById("dialog_border_2");
	var navHeadimg = document.getElementsByClassName("navHeadimg")[0]
	var navAfterLoginBox = document.getElementsByClassName("navAfterLoginBox")[0]
	client_2.onmouseover = function() {
		dialog_border_2.style.display = "inline-block";
	};
	client_2.onmouseout = function() {
		dialog_border_2.style.display = "none";
	};
	dialog_border_2.onmouseover = function() {
		dialog_border_2.style.display = "inline-block";
	};
	dialog_border_2.onmouseout = function() {
		dialog_border_2.style.display = "none";
	};
	navHeadimg.onmouseover = function() {
		navAfterLoginBox.style.display = "inline-block";
	};
	navHeadimg.onmouseout = function() {
		navAfterLoginBox.style.display = "none";
	};
	navAfterLoginBox.onmouseover = function() {
		navAfterLoginBox.style.display = "inline-block";
	};
	navAfterLoginBox.onmouseout = function() {
		navAfterLoginBox.style.display = "none";
	};
	classification_id = GetUrlByParamName("classification_id")
	if(classification_id == null) {
		classification_id = "-2"
	}
	getCookie();
	createCateFuncBox();
}
$(document).ready(function() {
	$('[data-toggle="tooltip"]').tooltip();
});

function switchLoginType(a) {
	var hetaoCode = document.getElementById("hetaoCode")
	var hetaoAccount = document.getElementById("hetaoAccount")
	if(a.parentElement == hetaoAccount) {
		hetaoCode.style.display = "block"
		hetaoAccount.style.display = "none"
	} else if(a.parentElement == hetaoCode) {
		hetaoCode.style.display = "none"
		hetaoAccount.style.display = "block"
	}
}

var user_token
var user_id
var pageNum = 1
var pageSize = 3
var classification_id
var order = "最新"
var start
var end
var totalPage

function GetUrlByParamName(name)   {    
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");    
	var URL =  decodeURI(window.location.search);    
	var r = URL.substr(1).match(reg);    
	if(r != null) {       //decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码
		return  decodeURI(r[2]);    
	};    
	return null;  
}

function changeBeSeletedClassification(id) {
	for(var i = 0; i < document.getElementById("courseClassifyList").children.length; i++) {
		document.getElementById("courseClassifyList").children[i].children[0].children[0].classList.remove("courseBeSelected")
	}
	var tag = document.getElementById(id).children[0]
	tag.classList.add("courseBeSelected")
}

function getCookie() {
	var loginBoolean = $.cookie('login_cookie');
	if(loginBoolean) {
		//JSON.parse()  将字符串解析为对象，方便使用
		user_token = loginBoolean
		document.getElementById("gotoPersonal").href = "http://localhost/page/personalPage/personalPage.html?token=" + user_token
		login_hide();
		queryPersonalMessage(user_token);
	} else {}
}

function changeListOrder(listOrder) {
	order = listOrder
	if(order == "最新") {
		document.getElementById("最新").classList.add("tagSelected")
		document.getElementById("热门").classList.remove("tagSelected")
	} else {
		document.getElementById("最新").classList.remove("tagSelected")
		document.getElementById("热门").classList.add("tagSelected")
	}
	getCourseClassification(classification_id)
}

function login_hide() {
	var navBeforeLogin = document.getElementById("navBeforeLogin")
	var navAfterLogin = document.getElementById("navAfterLogin")
	navBeforeLogin.style.display = "none"
	navAfterLogin.style.display = "inline-block"
	$('#exampleModal').modal('hide')
}

function quit() {
	window.event.returnValue = false
	$.cookie('login_cookie', "", {
		expires: -1,
		path: '/',
	})
	window.location.href = "http://localhost/page/LoginPage/Login.html";
}

function login() {
	var mobile = document.getElementById("inputCount").value
	var password = document.getElementById("inputPassword").value
	var data = {
		"mobile": mobile,
		"password": password,
	}
	$.ajax({
		type: "post",
		url: "http://localhost/zte-api/api/login",
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			console.log(r)
			if(r.msg != "登录成功") {
				alert(r.msg)
			} else {
				$.cookie('login_cookie', r.data.token.token, {
					expires: 123456789,
					path: '/',
				})
				console.log($.cookie("login_cookie"))
				location.reload()
			}
		},
		error: function() {
			alert("error");
		}
	});
}

function queryPersonalMessage(a) {
	var data = {
		"token": a,
	}
	$.ajax({
		type: "POST",
		url: "http://localhost/zte-api/api/personal/getUserAndCount?token=" + a,
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			console.log(r)
			user_id = r.data.userId
			document.getElementById("my-img").src = r.data.headImg
		},
		error: function() {
			alert("error");
		}
	});
}

function createCateFuncBox() {
	var list = ['fa fa-television', 'fa fa-globe', 'fa fa-briefcase', 'fa fa-etsy', 'fa fa-graduation-cap', 'fa fa-users', 'fa fa-signal',"fa fa-male","fa fa-book"];

	function getCourseClassificationForCateFuncBox() {
		var data = -2
		$.ajax({
			type: "get",
			url: "http://localhost/zte-api/api/course/getClassification?parentId=" + data,
			contentType: "application/json",
			data: JSON.stringify(data),
			success: function(r) {
				var html = fu(r.data, list)
				document.getElementById("courseClassifyList").innerHTML = html
				changeBeSeletedClassification(classification_id);
				getCourseClassification(classification_id);
			},
			error: function() {
				alert("error");
			}
		});
	}

	function fu(atr, icon) {
		var str = "";
		var mark = 0;
		for(var i = 0; i < atr.length; i++) {
			if(i == 0) {
				str += '<li>'
				str += '<a id="-2" target="_self" onclick="changeCourseClassification(this.id)">'
				str += '<span class="courseBeSelected">'
				str += '<span class="fa fa-th-large"></span><span> 全部</span>'
				str += '</span>'
				str += '</a>'
				str += '</li>'
			}
			str += '<li>'
			str += '<a id="' + atr[i].id + '" target="_self" onclick="changeCourseClassification(this.id)">'
			str += '<span>'
			str += '<span class="' + icon[i] + '"></span><span> ' + atr[i].name + '</span>'
			str += '</span>'
			str += '</a>'
			str += '</li>'
		}
		return str;
	}
	getCourseClassificationForCateFuncBox();
}

function changeCourseClassification(id) {
	pageNum = 1
	getCourseClassification(id)
}

function getCourseClassification(classificationId) {
	var data = classificationId
	classification_id = classificationId
	changeBeSeletedClassification(classificationId)
	document.getElementById("courseCardList").innerHTML = ""
	$.ajax({
		type: "get",
		url: "http://localhost/zte-api/api/course/getClassification?parentId=" + classificationId,
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			str = ""
			if(r.data.length == 0) {
				getCourseAndTeacherName(classificationId)
			} else {
				getCourseAndTeacherName(classificationId)
				getCourseAndTeacherName_child(r.data, classificationId)
			}
		},
		error: function() {
			alert("error");
		},
	});
}

function getCourseAndTeacherName_child(a, classificationId) {
	if(classificationId == "-2") {
		var data = {
			"pageNum": pageNum,
			"pageSize": pageSize,
			"order": order
		}
		$.ajax({
			type: "post",
			url: "http://localhost/zte-api/api/course/getAllCourseByOnce",
			contentType: "application/json",
			data: JSON.stringify(data),
			success: function(r) {
				if(r.courseList.list.length != 0) {
					createCourseList(r.courseList.list)
					start = r.courseList.start
					end = r.courseList.end
					totalPage = r.courseList.totalPage
					createPageBox()
				} else {
					checkCourseList()
				}
			},
			error: function() {
				alert("error");
			},
		});
	} else {
		for(var i = 0; i < a.length; i++) {
			if(a[i].list.length > 0) {
				getCourseAndTeacherName(a[i].id)
				getCourseAndTeacherName_child(a[i].list)
			}
			var data = {
				"pageNum": pageNum,
				"pageSize": pageSize,
				"classificationId": '' + a[i].id + '',
				"order": order
			}
			$.ajax({
				type: "post",
				url: "http://localhost/zte-api/api/course/getCourseAndTeacherName",
				contentType: "application/json",
				data: JSON.stringify(data),
				success: function(r) {
					if(r.courseList.list.length != 0) {
						createCourseList(r.courseList.list)
						start = r.courseList.start
						end = r.courseList.end
						totalPage = r.courseList.totalPage
						createPageBox()
					} else {
						checkCourseList()
					}
				},
				error: function() {
					alert("error");
				},
			});
		}
	}
}

function getCourseAndTeacherName(a) {
	var data = {
		"pageNum": pageNum,
		"pageSize": pageSize,
		"classificationId": '' + a + '',
		"order": order
	}
	$.ajax({
		type: "post",
		url: "http://localhost/zte-api/api/course/getCourseAndTeacherName",
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(r) {
			if(r.courseList.list.length != 0) {
				createCourseList(r.courseList.list)
				start = r.courseList.start
				end = r.courseList.end
				totalPage = r.courseList.totalPage
				createPageBox()
			} else {
				checkCourseList()
				createPageBox()
			}
		},
		error: function() {
			alert("error");
		},
	});
}
var str = ""

function createCourseList(courseList) {
	for(var i = 0; i < courseList.length; i++) {
		str += '<div class="courseCardBox f-cb" onclick="gotoCourseDetail(this.id)" id="' + courseList[i].id + '">'
		str += '<div class="courseCardImg">'
		str += '<div class="courseCard">'
		str += '<img src="' + courseList[i].coverPath + '" alt="' + courseList[i].title + '" height="150px" width="265px">'
		str += '</div>'
		str += '</div>'
		str += '<div class="courseCardMessageBackground">'
		str += '<div class="courseCardMessageBox">'
		str += '<div class="courseCardMessage">'
		str += '<div class="courseCardTitleBox f-cb f-f0">'
		str += '<a href="#" target="_blank">'
		str += '<span class="courseCardTitle">' + courseList[i].title + '</span>'
		str += '</a>'
		str += '</div>'
		str += '<div class="courseCardFrom f-f0">'
		str += '<span href="#" target="_blank" class="courseCardSchool f-fc9">课程讲师</span>'
		str += '<a href="#" target="_blank" class="f-fc9">' + courseList[i].teacherName + '</a>'
		str += '</div>'
		str += '<span class="courseCardDeclare f-f0 f-cb">'
		str += courseList[i].courseMustKnow
		str += '</span>'
		str += '<div class="courseCardFotter f-f0 f-fc3">'
		str += '<span class="fa fa-star-o f-fc9" style="width: 12px;height: 12px;font-size: 12px;"></span>'
		str += '<span class="courseCardPeople f-fc9">' + courseList[i].collectionCount + '人收藏</span>'
		str += '</div>'
		str += '</div>'
		str += '</div>'
		str += '</div>'
		str += '</div>'
	}
	document.getElementById("courseCardList").innerHTML = str
}

function checkCourseList() {
	var courseList = document.getElementById("courseCardList")
	if(courseList.children.length == 0) {
		var noneStr = ""
		noneStr += '<div class="noCourseShow">'
		noneStr += '<div class="noCourseShowText f-f0">抱歉，目前没有课程</div>'
		noneStr += '</div>'
		courseList.innerHTML = noneStr
	}
}

function createPageBox() {
	var pageBox = document.getElementById("pageBox")
	var option = ""
	if(totalPage == undefined) {
		document.getElementById("pageBoxBackground").style.backgroundColor = "white"
	} else {
		document.getElementById("pageBoxBackground").style.backgroundColor = "#f7f7f7"
		option += '<li class="PageUpBox">'
		option += '<a onclick="changePage(1)" class="PageUp">第一页</a>'
		option += '</li>'
		for(var i = start; i <= end; i++) {
			if(i == pageNum) {
				option += '<li class="whichPage">'
				option += '<span class="PageBeSelected" id="' + i + '">' + i + '</span>'
				option += '</li>'
			} else {
				option += '<li class="whichPage">'
				option += '<a onclick="changePage(this.id)" id="' + i + '">' + i + '</a>'
				option += '</li>'
			}
		}
		option += '<li class="PageDownBox">'
		option += '<a onclick="changePage(' + totalPage + ')" class="PageDown">末尾页</a>'
		option += '</li>'
		pageBox.innerHTML = option
	}
}

function changePage(id) {
	pageNum = parseInt(id)
	getCourseClassification(classification_id)
}

function gotoCourseDetail(courseId) {
	var position = "http://localhost/page/coursePage/courseDetails.html?courseId=" + courseId
	window.open(position)
}